package com.zhiyou100.service;

import java.sql.Timestamp;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyuncs.exceptions.ClientException;
import com.zhiyou100.dao.UserDao;
import com.zhiyou100.exception.UserException;
import com.zhiyou100.model.User;
import com.zhiyou100.util.SMSUtil;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public void findByCode(String phone) {

		User user = new User();
		
		User savedUser = userDao.findCTByPhone(phone);
			
		if(savedUser != null) {
		
			if(savedUser.getCode() != null) {
		
		Date now = new Date();

		long number = (now.getTime() - savedUser.getCodeSendTime().getTime()) / 1000;
			
		if(number < 60) {
			
			throw new UserException(402001, "一分钟内只能发送一条验证短信");
		}else {
			
			Timestamp time = new Timestamp(System.currentTimeMillis());
			
			savedUser.setCodeSendTime(time);
			
		}
		
		
		}else {
		
		int random = (int) ((Math.random() * (99999 - 10000 + 1)) + 10000);
	
		try {
			SMSUtil.SMSUtils(phone, random);
		} catch (ClientException e) {
			e.printStackTrace();

			System.out.println("短信发送失败，请稍后再试");
		}
		
		savedUser.setCode(random);
		
			}
		
		savedUser.setPhone(phone);
		
		userDao.update(savedUser);
		
			
		}else{
			
			int random2 = (int) ((Math.random() * (999999 - 100000 + 1)) + 100000);
			
			try {
				SMSUtil.SMSUtils(phone, random2);
			} catch (ClientException e) {
				e.printStackTrace();

				System.out.println("短信发送失败，请稍后再试");
			}
			
			
			user.setCode(random2);
			
			Timestamp time2 = new Timestamp(System.currentTimeMillis());
			
			user.setCodeSendTime(time2);
			
			user.setPhone(phone);
			
			userDao.add(user);
		}
		
		
	}

	@Override
	public User logina(User user) {

		User savedUser = userDao.findCTNByPhone(user.getPhone());

		if (savedUser == null) {

			throw new UserException(403001, "手机号输入有误");
		}else {

		Date now = new Date();

		long number = (now.getTime() - savedUser.getCodeSendTime().getTime()) / 1000;
		if (number > 300) {

			throw new UserException(403002, "验证码失效");
		}
		
		if(!savedUser.getCode().equals(user.getCode())) {
			
			throw new UserException(403003, "验证码输入有误");
		}
		
		if(savedUser.getName() == null) {
			
			int random = (int) ((Math.random() * (99999 - 10000 + 1)) + 10000);
			String str = "";
			str = random + "";

			savedUser.setName(str);
			savedUser.setPhone(user.getPhone());
			
			userDao.updateName(savedUser);
				}
		
		
		}
		return savedUser;
	}

	@Override
	public boolean loginb(User user) {
		
		User savedUser = userDao.findPassword(user.getPhone());
		
		if(savedUser == null) {
			
			throw new UserException(404001, "手机号输入错误");
		}
		if(savedUser.getPassword() == null) {
			
			throw new UserException(404002, "未修改密码");
		}
		if(!savedUser.getPassword().equals(user.getPassword())) {
			
			throw new UserException(404003, "密码错误");
			
		}
		return true;
	}

	@Override
	public User findAll(int id) {
		
		
		
		
		return  userDao.findAll(id);
	}

	@Override
	public void update(User user) {  

//		User savedUser = user.getId();
	
		userDao.updateById(user);
		
	}

}
